#include <iostream>
#include <functional>

class MyListener
{
public:
    using CallbackFunction = std::function<void(const std::string)>;

    void setCallback(CallbackFunction callback) {
        callback_ = callback;
    }

    // 模拟事件发生并调用回调函数
    void simulateEvent(const std::string& data) {
        std::cout << "Event occurred in MyListener ..." << std::endl;

        //调用回调函数
        if (callback_) {
            callback_(data);
        }
    }

private:
    CallbackFunction callback_;
};

class MyClassWithListener
{
private:
    MyListener listener;
public:
    void setListenerCallback(MyListener::CallbackFunction callback) {
        listener.setCallback(callback);
    }

    void performTask(const std::string& data) {
        std::cout << "Performing task..." << std::endl;

        listener.simulateEvent(data);
    }
};


int main() {
    std::cout << "sizeof MyClassWithListener : " << sizeof(MyClassWithListener) << std::endl;
    // MyClassWithListener obj;

    // obj.setListenerCallback([](const std::string& data) {
    //     std::cout << "Callback with data: " << data << std::endl;
    // });

    // obj.performTask("my data !");


    return 0;
}